package com.zbc.manager;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.*;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import com.zbc.config.MinioConfig;
import com.zbc.config.TencentCosClient;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.UploadObjectArgs;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * 通用的文件上传与下载
 * @author fangyu
 * @version v1.0.0
 * @since 2025/9/2 13:54
 */
@Component
@RequiredArgsConstructor
public class CosManager {
    private final TencentCosClient tencentCosClient;
    private final COSClient cosClient;
    private final MinioConfig minioConfig;
    private final MinioClient minioClient;


    /**
     * 上传本地文件到 COS
     *
     * @param key  对象键, 域名后面的部分
     * @param file 本地文件
     * @return COS 客户端
     */
    public PutObjectResult putObject(String key, File file) {
        String bucketName = tencentCosClient.getBucket();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
        // 设置存储类型, 默认标准存储(Standard), 可选低频访问存储(Standard_IA)
        putObjectRequest.setStorageClass(StorageClass.Standard);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 从 COS 下载文件到本地
     *
     * @param key 对象键, 域名后面的部分
     * @return COS 客户端
     */
    public COSObject getObject(String key) {
        String bucketName = tencentCosClient.getBucket();
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        return cosClient.getObject(getObjectRequest);
    }

    /**
     * 上传图片到 COS, 并使用数据万象进行解析图片的基本信息
     *
     * @param key  对象键, 域名后面的部分
     * @param file 本地文件
     * @return COS 客户端
     */
    public PutObjectResult putPictureObject(String key, File file) {
        String bucketName = tencentCosClient.getBucket();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
        // 对图片进行处理(获取基本信息也是一种处理)
        PicOperations operations = new PicOperations();
        // 1: 返回原图信息, 0: 不返回原图信息(默认)
        operations.setIsPicInfo(1);
        // 设置数据万象处理参数
        putObjectRequest.setPicOperations(operations);
        return cosClient.putObject(putObjectRequest);
    }

    /**
     * 上传图片到 Minio, 并使用数据万象进行解析图片的基本信息
     *
     * @param key  对象键, 域名后面的部分
     * @param file 本地文件
     * @return COS 客户端
     */
    public ObjectWriteResponse putPictureObject4Minio(String key, File file) throws Exception {
        String bucketName = minioConfig.getBucketName();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
        // 对图片进行处理(获取基本信息也是一种处理)
        PicOperations operations = new PicOperations();
        // 1: 返回原图信息, 0: 不返回原图信息(默认)
        operations.setIsPicInfo(1);
        // 设置数据万象处理参数
        putObjectRequest.setPicOperations(operations);
        return minioClient.uploadObject(
                UploadObjectArgs.builder()
                        .bucket(bucketName)
                        .object(key)
                        .filename(file.getAbsolutePath())
                        .build());
    }
}